package com.yuebao.httpTest;

import com.yuebao.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ChatBotClient {
    
    
    String appid;
    
    
    String password;
    
    @Resource
    RestTemplate restTemplate;
    
    @Resource
    StringRedisTemplate stringRedisTemplate;
    
    public ChatBotClient(String appid, String password) {
        this.appid = appid;
        this.password = password;
    }
    
    public String createToken(){
        String url = "http://110.249.135.178:8000/hblc/chatbot";
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("appid",appid);
        TokenResuestDTO body= TokenResuestDTO.builder()
                .appid(appid)
                .password(password)
                .build();
        HttpEntity<?> requestEntity=new HttpEntity<>(body,httpHeaders);
    
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
    
        String token = response.getHeaders().get("token").get(0);
        log.info(JsonUtils.writeValueAsString(token));
        stringRedisTemplate.opsForValue().set("chatbottoken",token,30, TimeUnit.DAYS);
        
        return token;
    }
    
    public String getToken(){
        return stringRedisTemplate.opsForValue().get("chatbottoken");
    }
    
    public String post(String url,Object param){
        
        String token=getToken();
        if (StringUtils.isEmpty(token)){
            token=createToken();
        }
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("appid",appid);
        httpHeaders.add("token",token);
        

        HttpEntity<?> requestEntity=new HttpEntity<>(param,httpHeaders);
        
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        
        if(!response.getStatusCode().equals(HttpStatus.OK)){
            token=createToken();
            return this.post(url,param);
        }
        
        return response.getBody();
    }
    
    public void chat(){
        String url="http://110.249.135.178:8000/hblc/chatbot";
    
        QueryDTO queryDTO=QueryDTO.builder()
        .sender("test")
        .message("你好")
        .authority("{'qx':['13102'],'gldw':'13102','yhmc':'ws2','ly':['00000'],'zy':['00000'],'iswxyy':'false'}")
        .timeStamp("2023-07-21 10:46:04")
        .build();
    
        log.info(JsonUtils.writeValueAsString(queryDTO));
        String res=post(url,queryDTO);
        
        log.info(JsonUtils.writeValueAsString(res));
    }
}
